package com.link.base.base.opty.controller;

import com.link.base.base.opty.model.OptyItem;
import com.link.base.base.opty.service.OptyItemService;
import com.link.base.base.product.model.Product;
import com.link.base.base.product.service.ProductService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.controller.BasicControllerException;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 商机行详情请求处理
 */
@Controller
@RequestMapping("/link/optyItem")
public class OptyItemController extends BasicController<OptyItem> {
    //private static final Logger logger = Logger.getLogger(PortalOptyItemController.class);

    @Resource
    private OptyItemService optyItemService;

    @Resource
    private ProductService productService;

    @Override
    public BasicService<OptyItem> getBasicService() throws Exception {
        return optyItemService;
    }

    @RequestMapping(value = "/queryListByOptyPage")
    @ResponseBody
    public Map<String, Object> queryListPage(Long optyId, @JsonParam QueryParams qps, HttpSession session,
                                             HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (null == optyId) {
                throw new BasicControllerException("optyId 不能为空！");
            }
            qps.addFilter(new Filter("optyId", String.valueOf(optyId)));
            OptyItem record = (OptyItem) BasicModel.transformClass(OptyItem.class, qps);
            List<OptyItem> list = getBasicService().queryByExamplePage(record);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(record, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/queryUnSelectProdPage")
    @ResponseBody
    public Map<String, Object> queryUnSelectProdPage(String optyId, @JsonParam QueryParams qps, HttpSession session,
                                                     HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (optyId == null || "".equals(optyId)) {
                throw new BasicControllerException("optyId 不能为空！");
            }
            Filter filter = new Filter("optyId", optyId);
            qps.addFilter(filter);
            OptyItem record = BasicModel.transformClass(OptyItem.class, qps);
            List<OptyItem> itemList = getBasicService().queryByExamplePage(record);
            List<Long> prodIdList = new ArrayList<>();
            qps.removeFilter(filter);
            Product t = BasicModel.transformClass(Product.class, qps);
            for (int i = 0; itemList != null && i < itemList.size(); i++) {
                prodIdList.add(itemList.get(i).getProdId());
            }
            t.setIds(prodIdList);
            List<Product> list;
            if (t.getIds().isEmpty()) {
                list = productService.queryByExamplePage(t);
            } else {
                list = productService.queryUnSelectedInOptyPage(t);
            }
            //qps.addFilter(new Filter("id",Filter.OP_NOT_IN,prodIdList));
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount(t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
